# import pyodbc module
# -*-coding:utf-8-*-
import pypyodbc


def queryDataFromTeradata(query):
    cur.execute(query)
    raw_data = cur.fetchall()
    print(raw_data)


# disable connection pooling
pypyodbc.pooling = False

# create connection
connection = pypyodbc.connect('DRIVER={Teradata};DBCNAME=172.26.160.36;UID=dbc;PWD=dbc;',
                              ansi=False,
                              unicode_results=True,
                              readonly=False,
                              timeout=2,
                              autocommit=True)

# enable auto commit
connection.autocommit = True

# python 3.x connect Teradata set charset（如果不设置此字符集，数据库连接无问题，但是执行SQL会无法执行）
# connection.setdecoding(pypyodbc.SQL_CHAR, encoding='gbk')
# connection.setdecoding(pypyodbc.SQL_WCHAR, encoding='utf-8')
# # connection.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le')
# connection.setencoding(encoding='utf-8')

cur = connection.cursor()

print("==================== show table ====================")
query = "show table nsodata.SAC_ISS_CNDT_TB"
queryDataFromTeradata(query)
print()
print()

print("==================== query table 1 ====================")
query = "select * from nsodata.AMAC_HUZY_GBK"
queryDataFromTeradata(query)
print()
print()

print("==================== query table 2 ====================")
query = "SELECT * FROM nsodata.SAC_ISS_CNDT_TB"
queryDataFromTeradata(query)
print()
print()

# print driver and database info
# print('-ODBC version        =', connection.getinfo(10))
# print('-DBMS name           =', connection.getinfo(17))
# print('-DBMS version        =', connection.getinfo(18))
# print('-Driver name         =', connection.getinfo(6))
# print('-Driver version      =', connection.getinfo(7))
# print('-Driver ODBC version =', connection.getinfo(77))

# disconnect
connection.close()
